"""权限管理模块模型 - 使用Django内建权限系统"""
from django.db import models
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.validators import RegexValidator


class PermissionCategory(models.Model):
    """权限分类模型 - 用于组织权限"""
    name = models.CharField(max_length=50, unique=True, help_text="分类名称")
    display_name = models.CharField(max_length=100, help_text="显示名称")
    description = models.TextField(blank=True, help_text="分类描述")
    order = models.IntegerField(default=0, help_text="排序")
    is_active = models.BooleanField(default=True, help_text="是否激活")
    created_at = models.DateTimeField(auto_now_add=True, help_text="创建时间")
    updated_at = models.DateTimeField(auto_now=True, help_text="更新时间")

    class Meta:
        db_table = 'permissions_category'
        verbose_name = '权限分类'
        verbose_name_plural = '权限分类'
        ordering = ['order', 'name']

    def __str__(self):
        return f"{self.display_name}({self.name})"


class GroupProfile(models.Model):
    """组扩展信息模型 - 扩展Django内建Group"""
    group = models.OneToOneField(
        Group,
        on_delete=models.CASCADE,
        related_name='profile',
        help_text="关联的组"
    )
    display_name = models.CharField(max_length=100, help_text="显示名称")
    description = models.TextField(blank=True, help_text="组描述")
    is_system = models.BooleanField(default=False, help_text="是否为系统组")
    created_at = models.DateTimeField(auto_now_add=True, help_text="创建时间")
    updated_at = models.DateTimeField(auto_now=True, help_text="更新时间")
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='created_groups',
        help_text="创建者"
    )

    class Meta:
        db_table = 'permissions_group_profile'
        verbose_name = '组扩展信息'
        verbose_name_plural = '组扩展信息'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.display_name}({self.group.name})"


class PermissionProfile(models.Model):
    """权限扩展信息模型 - 扩展Django内建Permission"""
    permission = models.OneToOneField(
        Permission,
        on_delete=models.CASCADE,
        related_name='profile',
        help_text="关联的权限"
    )
    display_name = models.CharField(max_length=100, help_text="显示名称")
    description = models.TextField(blank=True, help_text="权限描述")
    category = models.ForeignKey(
        PermissionCategory,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='permissions',
        help_text="权限分类"
    )
    is_system = models.BooleanField(default=False, help_text="是否为系统权限")
    order = models.IntegerField(default=0, help_text="排序")
    created_at = models.DateTimeField(auto_now_add=True, help_text="创建时间")
    updated_at = models.DateTimeField(auto_now=True, help_text="更新时间")

    class Meta:
        db_table = 'permissions_permission_profile'
        verbose_name = '权限扩展信息'
        verbose_name_plural = '权限扩展信息'
        ordering = ['category__order', 'order', 'permission__codename']

    def __str__(self):
        return f"{self.display_name}({self.permission.codename})"


class UserGroupAssignment(models.Model):
    """用户组分配记录模型 - 记录分配历史"""
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='group_assignments',
        help_text="用户"
    )
    group = models.ForeignKey(
        Group,
        on_delete=models.CASCADE,
        related_name='user_assignments',
        help_text="组"
    )
    assigned_at = models.DateTimeField(auto_now_add=True, help_text="分配时间")
    assigned_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='assigned_group_records',
        help_text="分配者"
    )
    expires_at = models.DateTimeField(null=True, blank=True, help_text="过期时间")
    is_active = models.BooleanField(default=True, help_text="是否激活")
    notes = models.TextField(blank=True, help_text="备注")

    class Meta:
        db_table = 'permissions_user_group_assignment'
        verbose_name = '用户组分配记录'
        verbose_name_plural = '用户组分配记录'
        ordering = ['-assigned_at']

    def __str__(self):
        return f"{self.user.username} - {self.group.name}"
